package NetEase;

import java.util.Scanner;

public class Main4 {
    public static void m1(){
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        int m=cin.nextInt();
        int[][] maxt=new int[n][m];
        int[][] dp=new int[n][m];
        dp[0][0]=0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                maxt[i][j]=cin.nextInt();
            }
        }
        System.out.println((n-1)+(m-1));
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        int m=cin.nextInt();
        int[][] maxt=new int[n][m];
        int[][] dp=new int[n][m];
        dp[0][0]=0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                maxt[i][j]=cin.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int up=0,left=0;
                if (i==0){
                    if (j!=0)dp[i][j]=dp[i][j-1]+(maxt[i][j]==maxt[i][j-1]?1:2);
                    else dp[i][j]=0;
                }else if (j==0){
                    dp[i][j]=dp[i-1][j]+(maxt[i][j]==maxt[i-1][j]?1:2);
                }else {
                        up=dp[i-1][j]+(maxt[i-1][j]==maxt[i][j]?1:2);
                        left=dp[i][j-1]+(maxt[i][j-1]==maxt[i][j]?1:2);
                        dp[i][j]=Math.min(up,left);
                }
            }
        }
        System.out.println(dp[n-1][m-1]);
    }
}
